#ifndef List_h
#define List_h


template <typename Type_T>
class List_T
{
	Type_T ** _arr;
    unsigned int _len;

public:
	List_T()
		: _arr(0)
		, _len(0)
	{};

	~List_T()
	{
		Free();
	};

	void Free()
	{
		if( _arr != 0 )
		{
			for( unsigned int ix = 0 ; ix < _len ; ix++ )
			{
				delete _arr[ix];
			}

			FREE(_arr);
		}
		_arr = 0;
		_len = 0;
	};

public:
	unsigned int Count()
	{
		return _len;
	};

    Type_T * Add(Type_T * tPtr)
	{
		try
		{
			if( _len == 0 )
			{
				_arr = (Type_T **)Memory_T::Alloc(sizeof(Type_T *));
			}
			else
			{
				_arr = (Type_T **)Memory_T::ReAlloc(_arr, (_len + 1) * sizeof(Type_T *));
			}

			_arr[_len++] = tPtr;
		}
		catch( Exception_T & )
		{
			delete tPtr;
			throw;
		}

		return tPtr;
	};

	Type_T * operator[](int ix)
	{
		return _arr[ix];
	};

	unsigned int IndexOf(const Type_T & t)
	{
		for( unsigned int ix = 0 ; ix < _len ; ix++ )
		{
			if( *_arr[ix] == t )
			{
				return ix;
			}
		}
		return _len;
	};

	unsigned int IndexOf(const Type_T & t, bool isCaseSensitive)
	{
		for( unsigned int ix = 0 ; ix < _len ; ix++ )
		{
			if( _arr[ix]->Equals(t, isCaseSensitive) )
			{
				return ix;
			}
		}
		return _len;
	};
};

#endif //List_h